package com.orhonit.ipb.portal.biz;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.orhonit.ipb.portal.entity.TbPartyOrgCatalog;
import com.orhonit.ipb.portal.mapper.GropToDeptMapper;
import com.orhonit.ipb.portal.mapper.TbPartyOrgCatalogMapper;
import com.orhonit.ipb.security.common.biz.BaseBiz;


/**
 * 党组织目录
 *
 * @author caodw
 * @email caodw@qq.com
 * @version 2018-04-23 11:25:03
 */
@Service
public class TbPartyOrgCatalogBiz extends BaseBiz<TbPartyOrgCatalogMapper,TbPartyOrgCatalog> {

	@Autowired
	private TbPartyOrgCatalogMapper tbPartyOrgCatalogMapper;
	@Autowired
	private GropToDeptMapper gropToDeptMapper;
	/**
	 * 通过userid查找当前组织id及上级组织id
	 * @param userId
	 * @return
	 */
	public String getTbPartyOrgCatalogByDeptId(String userId){
		return tbPartyOrgCatalogMapper.getTbPartyOrgCatalogByDeptId(userId);
	}
	
	/**
	 * @author  孙少辉
	 * @param sets 
	 * @date  2018年4月8日下午2:09:21
	 * @param deptIds
	 * @return
	 * @Description: 根据父查询子
	 */
	private Set<String> getParentIds(Set<String> sets, List<String> deptIds) {
		// TODO Auto-generated method stub
		List<String> flags = new ArrayList<>();
		for (String integer : deptIds) {
			List<String> lists = tbPartyOrgCatalogMapper.getIdFromParent(integer);
			if(lists.size() == 0){
				return sets;
			}
			for (String integer2 : lists) {
				sets.add(integer2);
				flags.add(integer2);
			}
		}
		return this.getParentIds(sets, flags);
	}
	/**
	 * @author  孙少辉
	 * @date  2018年4月8日下午2:00:39
	 * @param deptIds
	 * @return
	 * @Description: 拿到当前用户管理组织下的所有子组织
	 */
	public Set<String> getAllDeptList(List<String> deptIds) {
		// TODO Auto-generated method stub
		Set<String> sets = new HashSet<>();
		for (String integer : deptIds) {
			sets.add(integer);
		}
		Set<String> returnList = this.getParentIds(sets,deptIds);
		return returnList;
	}
	
	/**
	 * 通过组织id查找组织名称
	 * @param DeptId
	 * @return
	 */
	public String getDeptNameByDeptId(String DeptId){
		return tbPartyOrgCatalogMapper.getDeptNameByDeptId(DeptId);
	}
	/**
	 * 查找最高组织
	 * @return
	 */
	public TbPartyOrgCatalog getrootId(){
		return tbPartyOrgCatalogMapper.getrootId();
	}
	/**
	 * 通过组织编码查找下级所有组织
	 * @param upPOrganizationCode
	 * @return List<TbPartyOrgCatalog>
	 */
	public List<TbPartyOrgCatalog> getTbPartyOrgCatalogByparentId(String upPOrganizationCode){
		List<TbPartyOrgCatalog> list=tbPartyOrgCatalogMapper.getTbPartyOrgCatalogByparentId(upPOrganizationCode);
		  for(int i=0;i<list.size();i++){
			  int count=tbPartyOrgCatalogMapper.getTbPartyOrgCatalogByparentId(list.get(i).getPOrganizationCode()).size();
			  if(count>0){
				  list.get(i).setStatus(1);//有下级组织
			  }
			  else{
				  list.get(i).setStatus(0);//无下级组织
			  }
		  }
		return list;
	}
	
}